package com.azunitech.intercpt.ejb;

import javax.annotation.Resource;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

public class ActiveMQInterceptor {
	private static Logger logger = Logger.getLogger(ActiveMQInterceptor.class);
	
	@Resource(lookup = "CF-INTERCEPTOR-001")
	private QueueConnectionFactory connectionFactory;
	
	@Resource(lookup = "Queue-Interceptor-001")
	private Queue queue;
	
	private static final String SUBJECT="topic.jay";

	
	@AroundInvoke
	public Object context(InvocationContext ic) throws Exception {
		 try {
			 logger.info("This is called");
			 Send();
			 return ic.proceed();
		 }
		 finally{
			 
		 }
	}
	
	public void Send() throws JMSException{
		Connection connection = JMSConnection.getConnection();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		javax.jms.Destination destination = session.createQueue(SUBJECT);
		// Create the producer.
		MessageProducer producer = session.createProducer(destination);
		producer.setPriority(1);
		producer.setDeliveryMode(DeliveryMode.PERSISTENT);	
		String msg="ssdsdssddsdsds";
		TextMessage message = session.createTextMessage(msg);
		//System.out.println("Sending message: " + msg);
		producer.send(message);
		
	}
}
